#coding=utf-8
import mido
import matplotlib.pyplot as plt
import keras
import numpy as np
from mido import MidiTrack,MidiFile,Message
import librosa
import librosa.display
L=[[] for _ in range(16)]  #从其它歌曲中任意截取16个区间作为测试输入
'''
以下16条均是一些歌曲（不一定来源于该音乐家）的某片段
'''
L[0]=[66, 67, 68, 69, 70, 71, 72, 73, 74, 77, 75, 77, 82, 77, 75, 77, 84, 77, 77, 75, 77, 85, 84, 85, 84, 80, 77, 75, 80, 80, 73, 72, 73, 80, 73, 72, 73, 80, 80, 73, 72, 72, 68, 65, 65, 77, 75, 75, 77, 75]
L[1]=[83, 82, 83, 75, 82, 83, 75, 82, 83, 76, 82, 83, 78, 82, 83, 82, 78, 83, 78, 85, 78, 83, 78, 83, 87, 85, 78, 82, 85, 83, 75, 82, 83, 75, 82, 83, 76, 82, 83, 78, 82, 83, 85, 78, 83, 85, 78, 83, 85, 88]
L[2]=[69, 72, 70, 72, 70, 72, 70, 69, 67, 60, 74, 72, 72, 70, 72, 70, 69, 67, 66, 62, 60, 58, 57, 58, 62, 69, 67, 62, 65, 69, 69, 70, 69, 81, 82, 84, 62, 65, 72, 62, 65, 72, 62, 72, 70, 69, 64, 67, 65, 69]
L[3]=[69, 81, 88, 93, 90, 86, 83, 83, 83, 83, 83, 85, 85, 83, 81, 81, 73, 78, 88, 78, 86, 85, 86, 78, 81, 83, 74, 74, 76, 78, 79, 81, 76, 93, 88, 86, 81, 74, 73, 71, 69, 74, 81, 81, 81, 81, 83, 79, 78, 79]
L[4]=[73, 74, 78, 74, 73, 74, 81, 74, 73, 69, 66, 78, 76, 78, 76, 74, 73, 74, 76, 78, 78, 76, 78, 83, 78, 76, 78, 85, 78, 76, 78, 86, 85, 86, 85, 81, 78, 76, 81, 78, 81, 78, 76, 74, 73, 74, 71, 78, 71, 73]
L[5]=[85, 80, 82, 82, 80, 78, 80, 82, 85, 82, 85, 84, 85, 84, 82, 72, 74, 77, 78, 80, 78, 78, 77, 77, 68, 73, 80, 80, 78, 78, 77, 73, 68, 66, 73, 73, 80, 78, 73, 78, 76, 76, 75, 75, 73, 73, 75, 75, 75, 75]
L[6]=[70, 77, 75, 73, 75, 80, 75, 77, 85, 84, 80, 75, 80, 80, 73, 72, 73, 80, 73, 72, 73, 72, 68, 65, 77, 75, 75, 73, 72, 73, 70, 77, 75, 73, 75, 80, 80, 75, 77, 85, 84, 80, 75, 80, 80, 73, 82, 82, 80, 78]
L[7]=[80, 81, 83, 86, 85, 83, 81, 81, 80, 81, 76, 81, 80, 81, 76, 81, 80, 81, 76, 81, 80, 81, 88, 71, 76, 80, 81, 83, 85, 86, 81, 81, 81, 81, 81, 80, 76, 81, 80, 80, 81, 88, 86, 85, 83, 81, 80, 81, 76, 80]
L[8]=[68, 66, 66, 66, 68, 68, 68, 68, 68, 69, 61, 69, 69, 69, 61, 69, 69, 69, 61, 69, 69, 69, 61, 69, 69, 68, 64, 68, 66, 66, 66, 68, 68, 68, 68, 68, 69, 61, 69, 69, 69, 61, 69, 69, 69, 68, 69, 71, 76, 80]
L[9]=[75, 73, 75, 71, 71, 70, 71, 78, 71, 71, 70, 71, 66, 71, 71, 70, 71, 66, 71, 73, 75, 75, 76, 75, 76, 75, 73, 71, 70, 71, 71, 70, 71, 78, 71, 71, 70, 71, 66, 70, 71, 73, 75, 66, 71, 75, 73, 66, 70, 73]
L[10]=[86, 86, 95, 74, 76, 78, 81, 81, 78, 76, 74, 73, 73, 74, 64, 66, 67, 66, 67, 69, 71, 72, 74, 76, 78, 79, 81, 83, 83, 83, 78, 76, 76, 78, 81, 81, 81, 73, 76, 74, 73, 74, 78, 81, 79, 79, 74, 72, 83, 81]
L[11]=[79, 79, 72, 72, 84, 79, 72, 72, 84, 79, 72, 72, 77, 75, 79, 72, 72, 84, 79, 72, 72, 84, 79, 72, 72, 77, 79, 91, 84, 84, 96, 91, 84, 84, 96, 91, 84, 84, 89, 87, 75, 75, 74, 75, 77, 79, 79, 77, 79, 80]
L[12]=[77, 74, 75, 74, 67, 69, 70, 77, 75, 74, 72, 81, 74, 72, 74, 72, 70, 77, 77, 77, 77, 72, 72, 72, 77, 77, 77, 77, 72, 74, 77, 79, 59, 62, 67, 79, 77, 77, 70, 70, 77, 77, 72, 72, 70, 72, 74, 74, 74, 72]
L[13]=[70, 74, 77, 77, 77, 77, 70, 70, 67, 77, 77, 72, 72, 74, 75, 74, 62, 69, 62, 69, 77, 77, 70, 70, 67, 77, 77, 72, 72, 69, 70, 72, 74, 74, 74, 82, 74, 81, 77, 67, 77, 77, 70, 70, 67, 77, 77, 72, 72, 70]
L[14]=[60, 62, 63, 62, 62, 57, 58, 62, 55, 65, 65, 65, 65, 60, 60, 60, 65, 65, 65, 65, 60, 62, 63, 65, 58, 60, 62, 63, 55, 57, 58, 65, 63, 62, 60, 62, 63, 62, 60, 58, 65, 65, 65, 65, 60, 60, 60, 65, 65, 65]
L[15]=[82, 80, 79, 80, 79, 75, 75, 74, 75, 77, 79, 79, 77, 79, 80, 79, 77, 77, 75, 75, 75, 74, 75, 77, 79, 77, 79, 80, 82, 80, 79, 80, 79, 87, 86, 87, 89, 91, 91, 89, 91, 92, 91, 89, 89, 87, 87, 79, 72, 72]
Artist,Length,speed='V.K',50,12.5
Track,Music=MidiTrack(),MidiFile()
Model=keras.models.load_model(r"data\\Networks\\%s.h5"%(Artist))  #预加载神经网络

def Normalize(x):       #标准化辅助音符数据，>=0.5记作标签1，否则记作标签0
    if(x>=0.5):
        return 1
    else:
        return 0

def ModelOutput(Notes,order):
    Track,Music=MidiTrack(),MidiFile()
    Notes=np.array(Notes)
    Notes=np.reshape(Notes,(1,1,Length))        #转化为标准输入格式
    Result=Model.predict([Notes,Notes,Notes,Notes])
    Assistant=[Normalize(Result[0][0][x]) for x in range(Length,Length*2)]  #读取辅助音符数据
    Start=[int(abs(Result[0][0][x]*speed)) for x in range(Length*2,Length*3)] #读取音符开始延时数据
    Finish=[int(abs(Result[0][0][x]*speed)) for x in range(Length*3,Length*4)]   #读取音符结束延时数据
    for x in range(Length):
        if(Assistant[x]==0):    #含有辅助音符
            Track.append(Message("note_on",note=Notes[0][0][x],velocity=80,channel=0,time=Start[x])) #加入note_on 指令
            Track.append(Message("note_on",note=Notes[0][0][x]-6,velocity=80,channel=0,time=0))
            Track.append(Message("note_off",note=Notes[0][0][x],velocity=64,channel=0,time=Finish[x])) #加入note_off 指令
            Track.append(Message("note_off",note=Notes[0][0][x]-6,velocity=64,channel=0,time=0))        
        else:
            Track.append(Message("note_on",note=Notes[0][0][x],velocity=80,channel=0,time=Start[x]))
            Track.append(Message("note_off",note=Notes[0][0][x],velocity=64,channel=0,time=Finish[x]))
    Music.tracks.append(Track)
    Music.save("output%d.mid"%(order))
    
def NoteVisualize():
    xAxes=[x for x in range(50)]
    plt.subplots_adjust(wspace=0.3,hspace=0.3)
    for i in range(16):     #在每一个子图中绘制一组输入
        plt.subplot(4,4,i+1)
        plt.scatter(xAxes,L[i],s=5)
        plt.title("输入序列%d"%(i+1),fontsize=8)
    plt.show()

#def MidiVisualize
#NoteVisualize()

for i in range(16):
    ModelOutput(L[i],i+1)
print("风格迁移已经完成")
'''
接下来略去人工转化mid为wav的过程，文件名同名
'''

for i in range(16):     #导出结果
    y,sr=librosa.load(".\Result\\output%d.wav"%(i+1),sr=None)
    plt.subplot(4,4,i+1)
    plt.axis('off')
    plt.title('Audio%d'%(i+1))
    librosa.display.waveplot(y, sr)
plt.show()
